using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Medianamik.Core.Urls
{
    internal class UrlFastDomain : IEnumerable<IUrlDictionaryEntry>
    {
        Dictionary<Guid, UrlFastNodeDictionary> dictionaryByNodeId = new Dictionary<Guid, UrlFastNodeDictionary>();
        Dictionary<string, IUrlDictionaryEntry> dictionaryByUrl = new Dictionary<string, IUrlDictionaryEntry>();

        public int GetNumberOfDistinctEntries()
        {
            int numberOfDistinctEntries = 0;
            foreach (UrlFastNodeDictionary nodeDictionary in dictionaryByNodeId.Values)
            {
                numberOfDistinctEntries += nodeDictionary.GetNumberOfDistinctEntries();
            }
            return numberOfDistinctEntries;
        }

        public UrlFastNodeDictionary GetByNodeId(Guid nodeId)
        {
            return dictionaryByNodeId.ContainsKey(nodeId) ? dictionaryByNodeId[nodeId] : null;
        }

        public bool TryGetEntryByPath(string path, out IUrlDictionaryEntry result)
        {
            bool searchSuccess;
            string pathKey = path.ToLower();
            if (dictionaryByUrl.ContainsKey(pathKey))
            {
                result = dictionaryByUrl[pathKey];
                searchSuccess = true;
            }
            else
            {
                result = null;
                searchSuccess = false;
            }

            return searchSuccess;
        }

        public void Remove(IUrlDictionaryEntry entryToRemove)
        {
            if (dictionaryByUrl.ContainsKey(entryToRemove.LiteralRequestedUrl))
                dictionaryByUrl.Remove(entryToRemove.LiteralRequestedUrl);
            UrlFastNodeDictionary nodeDictionary = GetByNodeId(entryToRemove.NodeId);
            if (nodeDictionary != null)
            {
                if (nodeDictionary.ContainsKey(entryToRemove.LiteralRequestedUrl))
                {
                    nodeDictionary.Remove(entryToRemove.LiteralRequestedUrl);
                }
            }
        }

        public void AddEntry(IUrlDictionaryEntry entry)
        {
            string pathKey = entry.LiteralRequestedUrl.ToLower();
            dictionaryByUrl.Add(pathKey, entry);
            if (dictionaryByNodeId.ContainsKey(entry.NodeId))
            {
                dictionaryByNodeId[entry.NodeId].Add(entry.LiteralRequestedUrl, entry);
            }
            else
            {
                UrlFastNodeDictionary nodeDictionary = new UrlFastNodeDictionary();
                nodeDictionary.Add(pathKey, entry);
                dictionaryByNodeId.Add(entry.NodeId, nodeDictionary);
            }
        }

        /*public Dictionary<string, IUrlDictionaryEntry>.ValueCollection.Enumerator GetEnumerator()
        {
            return dictionaryByUrl.Values.GetEnumerator();
        }*/

        /*public Dictionary<string, IUrlDictionaryEntry>.ValueCollection AllUrl
        {
            get
            {
                return dictionaryByUrl.Values;
            }
            
        }*/

        #region IEnumerable<IUrlDictionaryEntry> Members

        IEnumerator<IUrlDictionaryEntry> IEnumerable<IUrlDictionaryEntry>.GetEnumerator()
        {
            return dictionaryByUrl.Values.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return dictionaryByUrl.Values.GetEnumerator();
        }

        #endregion
    }
}